<script lang="ts" setup>
import { checkUpdate, installUpdate } from '@tauri-apps/api/updater'
import { relaunch } from '@tauri-apps/api/process'

const notes = ref('update')
const loading = ref(false)

const u = async () => {
  notes.value = 'update start' 
  loading.value = true
  try {
    const { shouldUpdate } = await checkUpdate()
    notes.value = 'update message: ' + shouldUpdate
    if (shouldUpdate) {
      await installUpdate()
      await relaunch()
    }
    reset()
  } catch (error) {
    notes.value = 'update error: ' + error 
    reset()
  }
}

const reset = () => {
  notes.value = ''
  loading.value = false
}
</script>

<template>
  <h3>App Update Window</h3>
  <button @click="u" :style="{ color: loading ? 'blue' : 'black'}">检查更新</button>
  <p>{{ notes }}</p>
  <br /><br />
  <p>Page: /src/windows/update/App.vue</p>
</template>
